<#assign object = doc.object>
package ${object.package}.service.base;

import java.io.IOException;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import redora.rdo.api.BusinessRuleViolation;
import redora.rdo.exceptions.PersistenceException;
import ${object.package}.model.${object.name};
import ${object.package}.service.*;
import redora.rdo.util.BusinessRuleJSON;

/**
* This servlet dispatches standard retrieve / insert / update and delete operations on the ${object.name} object.
* This class is extended by ${object.name}Servlet, so if you want to modify the behavior of this class, please change that file.
* @see ${object.package}.service.http.${object.name}Servlet;
*/
public class ${object.name}ServletBase extends HttpServlet {
  private Logger l = Logger.getLogger("${object.package}.service.base.${object.name}ServletBase");
  private ${object.name}ServiceJSON serviceJSON;
  private ${object.name}Service service;

	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		try {
			<#if object.name == "RedoraConfiguration">
			//TODO move this to the final Servlet!
			Upgrade upgrade = new Upgrade();
			if (req.getParameter("_operationType").equalsIgnoreCase("upgradeTable")) {
				upgrade.upgradeTable(resp.getWriter());
			}
			if (req.getParameter("_operationType").equalsIgnoreCase("fetch")) {
				upgrade.initDefaultScript();
				serviceJSON.findAll(resp.getWriter());
				resp.getWriter().flush();
			}
			if (req.getParameter("_operationType").equalsIgnoreCase("executeScript")) {
				String scriptName = req.getParameter("scriptName");
				upgrade.executeUpgradeScript(scriptName);
			}
			<#else>
			if (req.getParameter("_operationType").equalsIgnoreCase("fetch")) {
				Map params = req.getParameterMap();
				if("true".equalsIgnoreCase(req.getParameter("findByParams")))
					serviceJSON.findByParams(params,resp.getWriter());
				else 
					serviceJSON.findAll(resp.getWriter());
				resp.getWriter().flush();
			} else if (req.getParameter("_operationType").equalsIgnoreCase("add")) {
				${object.name} ${object.name?uncap_first} = serviceJSON.fromJson(${object.name}Util.getJSONValue(req));
				Set<BusinessRuleViolation> ret = service.persist(${object.name?uncap_first});
				if (ret.isEmpty()) {
					${object.name}Util.copy(${object.name?uncap_first}, resp.getWriter());
				} else {
					BusinessRuleJSON.copy(ret, resp.getWriter());
				}
				resp.getWriter().flush();
			} else if (req.getParameter("_operationType").equalsIgnoreCase("update")) {
				${object.name} ${object.name?uncap_first} = serviceJSON.fromJson(${object.name}Util.getJSONValue(req));
				Set<BusinessRuleViolation> ret = service.persist(${object.name?uncap_first});
				if (ret.isEmpty()) {
					${object.name}Util.copy(${object.name?uncap_first}, resp.getWriter());
					//TODO create the copy function
				} else {
					BusinessRuleJSON.copy(ret, resp.getWriter());
				}
				resp.getWriter().flush();
			} else if (req.getParameter("_operationType").equalsIgnoreCase("remove")) {
				${object.name} ${object.name?uncap_first} = serviceJSON.fromJson(${object.name}Util.getJSONValue(req));
				Set<BusinessRuleViolation> ret = service.delete(${object.name?uncap_first});
				if (!ret.isEmpty()) {
					BusinessRuleJSON.copy(ret, resp.getWriter());
					resp.getWriter().flush();
				}
			}
			</#if>
		} catch (Exception e) {
			l.log(Level.SEVERE, "I failed to process a JSON request: " + req.getParameter("action"), e);
			throw new ServletException("I failed to process a JSON request: " + req.getParameter("action"), e);
		}
	}

  @Override
  public void init() throws ServletException {
    try {
      serviceJSON = ServiceFactory.${object.name?uncap_first}ServiceJSON();
      service = ServiceFactory.${object.name?uncap_first}Service();
    } catch (PersistenceException e) {
      l.log(Level.SEVERE, "Failed to initialize", e);
    }
    super.init();
  }
}
